package cn.fzkj.project.system.service.impl;

import cn.fzkj.common.utils.StringUtils;
import cn.fzkj.project.system.constant.CategoryByCons;
import cn.fzkj.project.system.domain.SysTodo;
import cn.fzkj.project.system.mapper.SysTodoMapper;
import cn.fzkj.project.system.service.ISysTodoService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/** 待办事项 服务层实现 */
@Service
public class SysTodoServiceImpl implements ISysTodoService {

    @Autowired
    private SysTodoMapper todoMapper;

    /** 批量插入待办事项 */
    @Override
    public int batchInsertTodos(List<SysTodo> todos) {
//        List<SysTodo> collect = todos.stream().map(item -> {
//            item.setUserId(userId);
//            return item;
//        }).collect(Collectors.toList());
//        collect.forEach(System.out::println);
        return todoMapper.batchInsertTodos(todos);
    }

    /** 删除 */
    @Override
    public int deleteTodoById(Long id, Long userId) {
        return todoMapper.deleteTodoById(id, userId);
    }

    /** 批量删除 */
    @Override
    public int batchDelTodos(Long[] ids, Long userId) {
        return todoMapper.batchDelTodos(ids, userId);
    }

    /** 按日期查询 */
    @Override
    public List<SysTodo> selectTodosByDate(String date, Long userId) {
        return todoMapper.selectListByDate(date, userId);
    }

    /** 新增 */
    @Override
    public int insertTodo(SysTodo todo) {
        return todoMapper.insertTodo(todo);
    }

    /** 更新状态 */
    @Override
    public int updateTodoStatus(SysTodo todo) {
//        todo.setDone(todo.getDone() == "1" ? "0" : "1");
        return todoMapper.updateTodoStatus(todo);
    }

    /** 获取全部数据 */
    @Override
    public List<SysTodo> selectList(SysTodo todo) {
        return todoMapper.selectList(todo);
    }

    /** 获取数量 */
    @Override
    public List<Map<String, Object>> selectListCount(Long userId) {
        return todoMapper.selectListCount(userId);
    }

    /** 获取未完成的*/
    @Override
    public List<Map<String, Object>> selectListUnDone(String category, Long userId) {
        if (StringUtils.isEmpty(category) ||
        StringUtils.equals(CategoryByCons.WEEK, category))
            return completion(todoMapper.selectListUnDoneByWeek(userId));
        if (StringUtils.equals(CategoryByCons.MONTH, category))
            return completion(todoMapper.selectListUnDoneByWeek(userId));
        return null;
    }

    /** 获取完成的 */
    @Override
    public List<Map<String, Object>> selectListDone(String category, Long userId) {
        if (StringUtils.isEmpty(category) ||
                StringUtils.equals(CategoryByCons.WEEK, category))
            return completion(todoMapper.selectListDoneByWeek(userId));
        if (StringUtils.equals(CategoryByCons.MONTH, category))
            return completion(todoMapper.selectListDoneByWeek(userId));
        return null;
    }

    /** 补全没有的天 */
    private synchronized List<Map<String, Object>> completion(List<Map<String, Object>> data){
        Set<String> news = new HashSet<>();
        news.add("Mon");
        news.add("Tue");
        news.add("Wed");
        news.add("Thu");
        news.add("Fri");
        news.add("Sat");
        news.add("Sun");
        Set<String> old = new HashSet<>();
        // 将数据库中查询出来的所有天数添加到set集合中
        data.forEach(item -> old.add((String)item.get("time")));
        // 从静态的set集合中移除查询出来的集合的数据
        news.removeAll(old);
        // 剩下的就是需要补0的
        Map map = new HashMap();
        news.forEach(item -> {
            map.put("time", item);
            map.put("count", 0);
            data.add(map);
        });
        return data;
    }

}
